package com.ym.medical.service.impl;

import cn.hutool.core.lang.UUID;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.ServiceTermsDao;
import com.ym.medical.domain.entity.ServiceTermsEntity;
import com.ym.medical.domain.param.ServiceTermsQueryParam;
import com.ym.medical.domain.param.ServiceTermsUpdateParam;
import com.ym.medical.domain.vo.ServiceTermsListPcVO;
import com.ym.medical.service.IServiceTermsSV;
import com.ym.medical.service.ISysDictSV;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ServiceTermsSVImpl extends BaseSVImpl<ServiceTermsEntity, ServiceTermsDao> implements IServiceTermsSV {


    @Autowired
    private ISysDictSV iSysDictSV;

    /**
     * 增加
     *
     * @param updateParam
     * @return
     */
    @Override
    public Long insertServiceTerms(ServiceTermsUpdateParam updateParam) {
        /*服务条款编号*/
        //查询字典是否存在该枚举
//        String name = serviceTerms.getTermsNameName();
//        SysDictModel model = SysDictModel.builder()
//                .dictCode("ServiceTermsEnum").itemName(name).build();
//        SysDict sysDict = iSysDictSV.selectSingleByModel(model);
//        SysDictModel model2 = SysDictModel.builder().includeColumns("MAX(item_code) item_code")
//                .queryByWhereClause("where dict_code = 'ServiceTermsEnum'").build();
//        SysDict sysDict2 = iSysDictSV.selectSingleByModel(model2);
//        String code = String.format("%02d", Integer.valueOf(sysDict2.getItemCode()) + 1);
//        if (null == sysDict) {
//            //插入一条字典
//
//            SysDictModel model3 =
//                    SysDictModel.builder().dictName("法律条款")
//                            .dictCode("ServiceTermsEnum").itemName(name)
//                            .itemCode(code)
//                            .createtime(new Date()).build();
//            iSysDictSV.addSelective(model3);
//        }
        updateParam.setTermsCode(UUID.fastUUID().toString());
        ServiceTermsEntity termsEntity = updateParam.covert();
        insert(termsEntity);
        return termsEntity.getId();
    }

    /**
     * 修改
     *
     * @param updateParam
     * @return
     */
    @Override
    public int updateServiceTerms(ServiceTermsUpdateParam updateParam) {
        ServiceTermsEntity serviceTerm = selectServiceTermsById(updateParam.getId());
        if (serviceTerm == null) {
            throw new BusinessException("该服务条款不存在");
        }
        if (StringUtils.isBlank(serviceTerm.getTermsName())) {
            throw new BusinessException("termsName为空");
        }
//        String name = serviceTerms.getTermsName();
//        SysDictModel model = SysDictModel.builder()
//                .queryByWhereClause("where dict_code = 'ServiceTermsEnum' and item_code = '" + name + "'")
//                .build();
//        SysDict sysDict = iSysDictSV.selectSingleByModel(model);
//        if (!sysDict.getItemName().equals(serviceTerms.getTermsNameName())) {
//            SysDict sysDict1 = new SysDict();
//            sysDict1.setItemName(serviceTerms.getTermsNameName());
//            iSysDictSV.updateByMyModel(sysDict1, model);
//        }

        ServiceTermsEntity termsEntity = updateParam.covert();
        return updateById(termsEntity);
    }

    /**
     * 根据条件查询服务条款
     *
     * @param queryParam
     * @return
     */
    @Override
    public List<ServiceTermsListPcVO> selectListPC(ServiceTermsQueryParam queryParam) {
        //查询字典
//        String termsNameName = null;
//        if (serviceTerms.getTermsNameName() != null) {
//            termsNameName = serviceTerms.getTermsNameName();
//            SysDictModel model = SysDictModel.builder().includeColumns("item_code")
//                    .queryByWhereClause("where dict_code = 'ServiceTermsEnum' and item_name like concat('" + termsNameName + "','%')").build();
//            List<SysDict> dicts = iSysDictSV.selectByModel(model);
//            String[] code = new String[dicts.size()];
//            for (int i = 0; i < dicts.size(); i++) {
//                code[i] = dicts.get(i).getItemCode();
//            }
//            serviceTerms.setQueryByWhereClause("where terms_name in (" + org.apache.commons.lang.StringUtils.join(code, ",") + ")");
//        }
//        serviceTerms.setOrderByClause("create_time desc");
        List<ServiceTermsListPcVO> serviceTermsList = mapper.selectListPC(queryParam);
        return serviceTermsList;
    }

    /**
     * 根据id查询服务条款
     *
     * @param id
     * @return
     */
    private ServiceTermsEntity selectServiceTermsById(Long id) {
        ServiceTermsEntity serviceTerms = selectById(id);
        return serviceTerms;
    }


}
